//
//  ClustalW.m
//  iBioCAD
//
//  Created by Orbitus on 10/22/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "ClustalW.h"


@implementation ClustalW

- (void) awakeFromNib
{
	[self init];
}

- (id)init {
    self = [super init];
    [[NSNotificationCenter defaultCenter] addObserver:self 
											selector:@selector(finishedClustal:) 
												name:NSTaskDidTerminateNotification 
											object:nil];
	
	myClustal = nil; // This is a good time to initialize the pointer
	return self;
}

- (IBAction)Clustal:(id)sender
{
	[clustalButton setTitle:@"Running..."];
	NSLog(@"Running ClustalW...");
    [clustalButton setEnabled:NO];
	
	[appRunningIndicator startAnimation:self];
		
	myClustal=[[NSTask alloc] init];
	pipe=[[NSPipe alloc] init];
	NSFileHandle *handle;
	NSString *string;
	
	NSArray * dnaExportList = [dnaArrayController arrangedObjects];
	
	myDesktopFolderPath	=  @"/Sequences/iBioCAD/Desktop/sequence.fasta";
	/*NSString* myOutFilePath = [myDesktopFolderPath stringByAppendingPathComponent:@"Rudy.fasta"];
	
	int i;
	
 	id myString = @"";
	int tempCount;

	
	int ii=0,jj=0;
	/*tempCount = [dnaExportList count];
	
	for (i=0; i < tempCount; i++)
	{
		myString = [myString stringByAppendingString:[[[dnaExportList objectAtIndex:i]  valueForKey:@"title"]  stringByAppendingString:@"\n"] ];
	myString = [myString stringByAppendingString:[[[dnaExportList objectAtIndex:i]  valueForKey:@"sequence"]stringByAppendingString:@"\n"]];
		
		NSLog(@"myString=%@",myString);
	}
	NSLog(@"your String=%@",myString);
	[myString writeToFile:myOutFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
	*/
	
	
	
	myDesktopFolderPath	= @"/Sequences/iBioCAD/Desktop/sequence.fasta";
	clustalPath = @"/UnixApps/EMBOSS/emboss/emma";
	
	[myClustal setLaunchPath:clustalPath];
	[myClustal setCurrentDirectoryPath:myDesktopFolderPath];
	[myClustal setArguments:[NSArray arrayWithObjects:@"-sequence", myDesktopFolderPath ,@"-outseq", @"/Sequences/iBioCAD/Desktop/emma.fasta", @"-dendoutfile", @"/Sequences/iBioCAD/Desktop/emma.dnd", nil]];
	[myClustal setStandardOutput:pipe];
	
	
	myHandle=handle=[pipe fileHandleForReading];
	
	[myClustal launch];
	
	[NSThread detachNewThreadSelector:@selector(copyData:)
								 toTarget:self withObject:handle];
	
	    
    [clustalOutput setString:@"Finished ClustalW Alignment"];
	NSLog(@"`...Finished ClustalW!");
	
}
- (void)copyData:(NSFileHandle*)handle {
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
	NSString *string;
/*    NSData *data;
    while([data=[handle availableData] length]) { // until EOF (check reference)
        NSString *string=[[NSString alloc] initWithData:data
																				encoding:NSASCIIStringEncoding];
        NSRange theEnd=NSMakeRange([[clustalOutput string] length],0);
		
        [clustalOutput replaceCharactersInRange:theEnd
														   withString:string]; // append new string to the end
        theEnd.location+=[string length]; // the end has moved
        [clustalOutput scrollRangeToVisible:theEnd];
		
        [string release];
    }*/
	//string=[[NSString alloc] initWithData:[handle readDataToEndOfFile] encoding:NSASCIIStringEncoding]; // convert NSData -> NSString
    
    [clustalOutput setString:@"Howdy Beggining Alignment"];
    [pool release];
}

- (void)finishedClustal:(NSNotification *)aNotification {
	//[clustalButton setTitle:@"ClustalW"];
	//[clustalButton setEnabled:YES];
	//[appRunningIndicator stopAnimation:self];
	//id string=[[NSString alloc] initWithData:[myHandle readDataToEndOfFile] encoding:NSASCIIStringEncoding];
    //[clustalOutput setString:string];

	//[myBlast release]; // Don't forget to clean up memory
    //[pipe release];
}

@end
